Passed
Pull Request — master (#23)
by
unknown
09:10 queued 04:58
created

NameBoxComponent.doOnChangeHTML   B

Complexity

Conditions 4

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 40
dl 0
loc 47
rs 8.92
c 0
b 0
f 0
1
import { SaveFileService } from './../../data/savefile.service';
2
import { TextService } from './../../data/text.service';
3
/**
4
   Copyright 2018 June Hanabi
5
6
   Licensed under the Apache License, Version 2.0 (the "License");
7
   you may not use this file except in compliance with the License.
8
   You may obtain a copy of the License at
9
10
       http://www.apache.org/licenses/LICENSE-2.0
11
12
   Unless required by applicable law or agreed to in writing, software
13
   distributed under the License is distributed on an "AS IS" BASIS,
14
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
   See the License for the specific language governing permissions and
16
   limitations under the License.
17
 */
18
19
import { Component, OnInit, Input, OnChanges } from '@angular/core';
20
21
declare var window: {
22
    require: any;
23
};
24
25
const _ = window.require("lodash");
26
27
@Component({
28
    selector: 'name-box',
29
    templateUrl: './name-box.component.pug',
30
    styleUrls: ['./name-box.component.scss'],
31
})
32
export class NameBoxComponent implements OnInit, OnChanges {
33
34
    constructor(
35
        public saveFile: SaveFileService,
36
        public textService: TextService
37
    ) {
38
    }
39
40
    ngOnInit() {
41
    }
42
43
    ngOnChanges() {
44
        this.doOnChangeHTML();
45
    }
46
47
    // Max Character count
48
    // All Pokemon have 10 character limit
49
    // All Trainers and People have a 7 character limit for dialog sake
50
    @Input()
51
    public maxLength: number = 10;
52
53
    @Input()
54
    public bg: boolean = false;
55
56
    @Input()
57
    public textBlack: boolean = false;
58
59
    // The template contains 3 parts to it
60
    // 1. A gameboy string to give context to the text from the textbox
61
    // 2. HTML such as line breaks (A textbox is 2 lines)
62
    // 3. An insertion point for the text from the textbox
63
    //
64
    // To use this the string recognizes special template code
65
    // Write HTML directly like this, mostly only used for linebreaks
66
    // For gameboy strings write text inside of <(c())> like this <(c(Hello))>
67
    // To insert the textbox text which is already compiled use this <<t>>
68
    //
69
    // <()> - Runs a script, there is only one function, c, to compile
70
    // <<>> - Inserts a variable, there is only 1 variable, t, the textbox value
71
    // <<<>>> - Inserts variable escaped, there if you ever need it
72
    @Input()
73
    public template: string = "<<t>>";
74
75
    // Latest HTML representation
76
    @Input()
77
    public value: string = "";
78
79
    protected compiled = null;
80
    public writing = "";
81
82
    public doOnChangeHTML() {
83
        let rivalName = this.saveFile.fileDataExpanded.rival.rivalName;
84
        if (rivalName == "")
85
            rivalName = "Blue"
86
87
        let playerName = this.saveFile.fileDataExpanded.player.basics.playerName;
88
        if (playerName == "")
89
            playerName = "Red"
90
91
        const event = this.textService.convertEngToHTML(
92
            this.value,
93
            this.maxLength,
94
            rivalName,
95
            playerName);
96
97
        const fontStr = event;
98
99
        const self = this;
100
        const c = (str: string) => {
101
            const val = self.textService.convertEngToHTML(
102
                str,
103
                1000, // Don't limit context wording
104
                rivalName,
105
                playerName);
106
107
            return val;
108
        }
109
110
        if (this.compiled === null)
111
112
            // Compile template if there isn't one compiled
113
            this.compiled = _.template(this.template, {
114
                // Interpolate <<Value>>
115
                interpolate: /<<([\s\S]+?)>>/g,
116
117
                // Evaluate <(Value)>
118
                evaluate: /<\(([\s\S]+?)\)>/g,
119
120
                // Escape <<<Value>>>
121
                escape: /<<<([\s\S]+?)>>>/g,
122
            });
123
124
        // @ts-ignore
125
        this.writing = this.compiled({
126
            c, // Translate function
127
            t: fontStr // Compiled text from textbox
128
        });
129
    }
130
}
131